package dba.example.dao.impl

import dba.example.dao.CompanyDao
import dba.example.repos.CompanyRepository
import org.springframework.data.domain.Pageable
import dba.example.entities.Company
import org.springframework.transaction.annotation.Transactional
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Repository
import javax.persistence.EntityManagerFactory

@Repository
class CompanyDaoImpl (val emf: EntityManagerFactory, val companyRepo: CompanyRepository) extends CompanyDao {

  @Transactional(readOnly = true)
  def list(page: Pageable): java.util.List[Company] = {
    companyRepo.listByName(page)
  }

  @Transactional(readOnly = true)
  def get(id: Integer): Company = {
    companyRepo.findOne(id)
  }

  @Transactional
  override def save(company: Company): Company = {
    val em = emf.createEntityManager()
    val savedCompany = em.merge(company)
    savedCompany
  }

}
